package org.example.mybatis.dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.mybatis.dimain.Order;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class OrderMapper {

    //这是我添加的修改方法
    public int updateOrder333(){
        return 0;
    }

//    static{
//
//
//    }

    public static void main(String[] args) {
        try {
           // new OrderMapper().selectAll();
            //测试查一条数据
            //new OrderMapper().selectOrderById(100569001);
            // 测试插入一条数据
//            Order order=new Order();
//            order.setHpId(2);
//            order.setSmId(1);
//            order.setOrderDate(new Date());
//            order.setUserId("123");
//            new OrderMapper().insertOrder(order);

            //更新一条order
//            Order order=new Order();
//            order.setHpId(1);
//            order.setSmId(4);
//            order.setOrderDate(new Date());
//            order.setUserId("12345672222");
//            order.setOrderId(100569848);
//
//            new OrderMapper().updateOrder(order);

            //删除一条数据
            new OrderMapper().deleteOrder(100569848);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //定义对所有订单进行查询的方法
    public List<Order> selectAll()throws Exception{

        List<Order> orderList=new ArrayList<>();

        //分两步

        //1.初始化mybatis的运行环境
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        //2.调用mybatis查询接口获取数据

        SqlSession sqlSession=sqlSessionFactory.openSession();

        orderList=sqlSession.selectList("order.selectAll");

        for(Order o:orderList){
            System.out.println(o.getOrderId()+","+o.getOrderDate());
        }


        //3.释放资源
        sqlSession.close();


        return orderList;
    }

    // 往订单表插入一条数据
    public int insertOrder(Order order)throws Exception{
        int rows=0;
        //1.初始化mybatis的运行环境
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        // 2.调用接口插入数据
        SqlSession sqlSession=sqlSessionFactory.openSession();
        rows=sqlSession.insert("order.insertOrder",order);
        sqlSession.commit();//必须提交
        System.out.println("rows:"+rows);
        System.out.println("orderId:"+order.getOrderId());

        //3.
        sqlSession.close();


        return rows;
    }

    //修改订单表中的某一条数据
    public int updateOrder(Order order)throws Exception{
        int rows=0;
        //1.初始化mybatis的运行环境
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        //2.调用接口
        SqlSession sqlSession=sqlSessionFactory.openSession();
        rows=sqlSession.update("order.updateOrder",order);

        sqlSession.commit();
        System.out.println("rows:"+rows);
        return rows;
    }

    //删除订单表中的一条数据
    public int deleteOrder(int orderId)throws Exception{
        int rows=0;
        //1.初始化mybatis的运行环境
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        //2.调用接口
        SqlSession sqlSession=sqlSessionFactory.openSession();
        rows=sqlSession.update("order.deleteOrder",orderId);

        sqlSession.commit();
        System.out.println("rows:"+rows);

        return rows;

    }

    //查询某条订单数据

    public Order selectOrderById(int orderId)throws Exception{

        Order order=null;

        //分两步

        //1.初始化mybatis的运行环境
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);


        //2.调用mybatis查询接口获取数据

        SqlSession sqlSession=sqlSessionFactory.openSession();

        order=sqlSession.selectOne("order.selectOne",orderId);

        System.out.println(order.getOrderId()+"，"+order.getOrderDate());


        //3.释放资源
        sqlSession.close();


        return order;
    }
}
